unit YahooStockDataDown;

interface

uses
  Classes, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, Dialogs, SysUtils;

type
  TYahooStockDataDown = class
  private
    HttpClient: TIdHTTP;
    FDownloadPath: String;
  public
    procedure DownloadData(AStockCode, AFileName: String);
    constructor Create(ADownloadPath: String);
    destructor Destroy();override;
  end;

implementation

{ TYahooStockDataDown }

constructor TYahooStockDataDown.Create(ADownloadPath: String);
begin
  HttpClient := TIdHTTP.Create(nil);
  FDownloadPath := ADownloadPath;
end;

destructor TYahooStockDataDown.Destroy;
begin
  HttpClient.Free;
  inherited;
end;

procedure TYahooStockDataDown.DownloadData(AStockCode, AFileName: String);
var
  MemoryStream: TMemoryStream;
  csvFile:TStringList;
begin
  MemoryStream:=TMemoryStream.Create;
  csvFile := TStringList.Create;
  try
    HttpClient.Get('http://table.finance.yahoo.com/table.csv?s=' + AStockCode,MemoryStream);
    MemoryStream.SaveToFile(FDownloadPath + AFileName + '.csv');

    csvFile.LoadFromFile(FDownloadPath + AFileName + '.csv');

    csvFile.Delete(0);

    csvFile.SaveToFile(FDownloadPath + AFileName + '.csv');
  finally
    MemoryStream.Free;
    csvFile.Free;
  end;
end;

end.
